package cn.yunyichina.provider.reservation.service.impl;

import cn.yunyichina.provider.framework.page.PageInfo;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.reservation.dao.MechanismDao;
import cn.yunyichina.provider.reservation.entity.Mechanism;
import cn.yunyichina.provider.reservation.service.MechanismService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by JoeZ on 2016/8/24.
 */
@Service
public class MechanismServiceImpl implements MechanismService{

    @Resource
    private MechanismDao mechanismDao;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public ResponseObject addMechanism(String argm) {
        ResponseObject responseObject = new ResponseObject();
        try{
            Mechanism mechanism = JsonUtils.parseObject(argm, Mechanism.class);
            mechanism.setCreateTime(simpleDateFormat.format(new Date()));
            mechanismDao.insert(mechanism);
            return responseObject;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ResponseObject updateMechanism(String argm) { ResponseObject responseObject = new ResponseObject();
        try{
            Mechanism exMechanism = JsonUtils.parseObject(argm, Mechanism.class);

//            Mechanism mechanism = mechanismDao.selectById(exMechanism.getId());
//            mechanism.setName(exMechanism.getName());
//            mechanism.setAddress(exMechanism.getAddress());
//            mechanism.setGrade(exMechanism.getGrade());
//            mechanism.setContactName(exMechanism.getContactName());
//            mechanism.setContactPhone(exMechanism.getContactPhone());

            mechanismDao.update(exMechanism);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return responseObject;
    }

    @Override
    public ResponseObject deleteMechanism(String argm) {
        ResponseObject responseObject = new ResponseObject();
        try{
            if(StringUtils.isNotBlank(argm)){
                Mechanism mechanism = mechanismDao.selectById(argm);
                mechanism.setStatus(2);
                mechanismDao.update(mechanism);
            }
            return responseObject;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ResponseObject findMechanism(String argm) {
        ResponseObject responseObject = new ResponseObject();
        Mechanism mechanism = mechanismDao.selectById(argm);
        responseObject.setResultObject(mechanism);
        return responseObject;
    }

    @Override
    public ResponseObject listMechanism(String argm) {
        ResponseObject responseObject = new ResponseObject();

        //增加条件
        Mechanism mechanism = new Mechanism();
        mechanism.setStatus(1);

        //查出总记录数
        Map<String , String> map = JSON.parseObject(argm, Map.class);
        long count = mechanismDao.selectCount(mechanism);

        if(StringUtils.isNotBlank(map.get("name"))){
            mechanism.setName(map.get("name"));
        }
        if(StringUtils.isNotBlank(map.get("provinceCode"))){
            mechanism.setProvinceCode(map.get("provinceCode"));
        }
        if(StringUtils.isNotBlank(map.get("cityCode"))){
            mechanism.setCityCode(map.get("cityCode"));
        }

        List<Mechanism> mechanisms = null;

        if(StringUtils.isBlank(map.get("pageNum")) && StringUtils.isBlank(map.get("pageSize"))){
            mechanisms = mechanismDao.select();
        }else{
            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(count);
            pageInfo.setPageNum(Integer.valueOf(map.get("pageNum")));
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize")));
            pageInfo.setPages((pageInfo.getTotal().intValue() + pageInfo.getPageSize() - 1) / pageInfo.getPageSize());
            mechanisms = mechanismDao.select(mechanism , pageInfo);
        }

        responseObject.setResultObject(mechanisms);
        return responseObject;
    }

    @Override
    public ResponseObject stopMechanism(String argm) {
        ResponseObject responseObject = new ResponseObject();
        try{
            Mechanism exMechanism = JsonUtils.parseObject(argm, Mechanism.class);

            Mechanism mechanism = mechanismDao.selectById(exMechanism.getId());
            mechanism.setStatus(exMechanism.getStatus());
            mechanismDao.update(mechanism);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return responseObject;
    }
}
